#ifndef __H__PALLOC_H__
#define __H__PALLOC_H__


#include "linux_def.h"
#include "core.h"

#define MAX_ALLOC_FROM_POOL	(getpagesize() - 1)
#define DEFAULE_POOL_SIZE	(16 * 1024)
#define	MIN_POOL_SIZE	(sizeof(pool_t) + 2 * sizeof(pool_large_t))

typedef void (*pool_cleanup_callback)(void *data);
typedef struct pool_cleanup_s pool_cleanup_t;

struct pool_cleanup_s{
	pool_cleanup_callback	handler;
	void	*data;	//要清楚的数据
	pool_cleanup_t	*next;	//下一个清楚点
};

typedef struct pool_large_s	pool_large_t;

struct pool_large_s{
	pool_large_t	*next;
	void		*alloc;
};

typedef struct{
	char	*last;	//当前内存分配的末端，也是下次开始的地方
	char	*end;	//内存池结束的地方
	char	*next;	//多块内存形成链表
	int	failed; //内存分配失败次数
}pool_data_t;

struct pool_s{	//头部结构
	char	*last;	//the pool for data
	char	*end;
	pool_t	*current;	//指向当前内存池
//	chain_t	*chain;		
	pool_t	*next;
	pool_large_t	*large;	//大块内存链表，分配内存大于max
	pool_cleanup_t	*cleanup;	//释放内存的callback
	log_t	*log;
};

typedef struct{
	int	fd;
	char	*name;
	log_t	*log;
}pool_cleanup_file_t;

pool_t	*create_pool(size_t size, log_t *log);
void	destroy_pool(pool_t *pool);

void	*palloc(pool_t *pool, size_t size);
void	*pcalloc(pool_t *pool, size_t size);
int	pfree(pool_t *pool, void *p);

pool_cleanup_t	*pool_cleanup_add(pool_t *pool, size_t size);
void pool_cleanup_file(void *data);
void pool_delete_file(void *data);

#endif
